# -*- coding: utf-8 -*-
'''
Created on 16-10-2012

@author: LONG HOANG GIANG
'''

from CrawlerLib import commonlib, html2text
from Model.storymodel import StoryModel
import re, os


class MaxReading():
    
    __url = ''
    
    def __init__(self, url):
        self.__url = url
        
    def detail(self, url):
        tree = commonlib.loadweb(url).build_tree()
        detailNode = tree.xpath("//div[@id='content']//div[@id='chapter']")
        content = html2text.html2text(commonlib.Etree.tohtml(detailNode[0])).strip()
        data = []
        start = end = 0
        ltitle = ''
        for m in re.finditer(r"<strong>([^<]+)</strong>", content):
            end = m.start()
            if ltitle == '': ltitle = m.group(1)
            if start == end == 0: continue
            data.append({'title': ltitle, 'content': re.sub(r"\n", "<br />", content[start:end])})
            start = end
            ltitle = m.group(1)
        data.append({'title': ltitle, 'content': re.sub(r"\n", "<br />", content[start:])})
        return data
            
    
    def getgiaidapyhoc(self):
        
        tree = commonlib.loadweb(self.__url).build_tree()
        if tree == None: return
        smodel = StoryModel('/giaidapyhoc')
        smodel.open()
        for chapter in tree.xpath("//table[@class='table-style01']//td/a"):
            link = chapter.get('href')
            data = self.detail(link)
            
            for item in data:
                cid = smodel.addChapter(unicode(commonlib.toUpper(item['title'])))
                smodel.addDetail(cid, item['content'])
            
        smodel.close()
            
            


if __name__ == '__main__':
    
    maxreading = MaxReading('http://maxreading.com/sach-hay/500-giai-dap-y-hoc')
    
    maxreading.getgiaidapyhoc()
    print '> Finished'
    os._exit(1)
    